package com.qxzn.app.page.model;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.AlbumPictureBean;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.ContactBean;
import com.qxzn.app.bean.UserRemindBean;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.util.AppUtil;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeModel {

    private Context context;
    private MyInterface.GetContactListener getContactListener;
    private MyInterface.GetUserRemindListListener getUserRemindListListener;
    private MyInterface.AddRemindListener addRemindListener;
    private MyInterface.UpdateRemindListener updateRemindListener;
    private MyInterface.DelRemindListener delRemindListener;
    private MyInterface.GetAlbumListListener getAlbumListListener;
    private MyInterface.DelAlbumPictureListener delAlbumPictureListener;
    private Call call;

    public HomeModel(Context context) {
        this.context = context;
    }

    /**
     * 获取通讯录包括自己
     *
     * @param params
     */
    public void getContact(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().getContact1(body);
        call.enqueue(new Callback<BaseBean<List<ContactBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<ContactBean>>> call, Response<BaseBean<List<ContactBean>>> response) {
                if (getContactListener != null) {
                    getContactListener.onGetContactsSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<List<ContactBean>>> call, Throwable t) {
                if (getContactListener != null) {
                    getContactListener.onGetContactsFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * app查询所有任务提醒
     *
     * @param params
     */
    public void getUserRemindList(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().getUserRemindList(body);
        call.enqueue(new Callback<BaseBean<List<UserRemindBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<UserRemindBean>>> call, Response<BaseBean<List<UserRemindBean>>> response) {
                if (getUserRemindListListener != null) {
                    getUserRemindListListener.onGetUserRemindListSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<List<UserRemindBean>>> call, Throwable t) {
                if (getUserRemindListListener != null) {
                    getUserRemindListListener.onGetUserRemindListFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 添加任务提醒
     *
     * @param params
     */
    public void addRemind(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().addRemind(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (addRemindListener != null) {
                    addRemindListener.onAddRemindSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (addRemindListener != null) {
                    addRemindListener.onAddRemindFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 更新任务提醒
     *
     * @param params
     */
    public void updateRemind(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().updateRemind(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (updateRemindListener != null) {
                    updateRemindListener.onUpdateRemindSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (updateRemindListener != null) {
                    updateRemindListener.onUpdateRemindFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 删除任务提醒
     *
     * @param params
     */
    public void delRemind(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().delRemind(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (delRemindListener != null) {
                    delRemindListener.onDelRemindSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (delRemindListener != null) {
                    delRemindListener.onDelRemindFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 用户查看图片列表
     *
     * @param params
     */
    public void getAlbumList(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().getAlbumList(body);
        call.enqueue(new Callback<BaseBean<List<AlbumPictureBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<AlbumPictureBean>>> call, Response<BaseBean<List<AlbumPictureBean>>> response) {
                if (getAlbumListListener != null) {
                    getAlbumListListener.onGetAlbumListSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<List<AlbumPictureBean>>> call, Throwable t) {
                if (getAlbumListListener != null) {
                    getAlbumListListener.onGetAlbumListFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 用户删除相册图片
     *
     * @param params
     */
    public void delAlbumPicture(JSONObject params) {
        if (params == null) {
            return;
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), params.toJSONString());
        call = HttpRequest.getService().delAlbumPicture(body);
        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (delAlbumPictureListener != null) {
                    delAlbumPictureListener.onDelAlbumPictureSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (delAlbumPictureListener != null) {
                    delAlbumPictureListener.onDelAlbumPictureFailure();
                }
                AppUtil.showToast(context, "网络错误，请稍后再试！");
                AppUtil.showLog("错误：" + t);
            }
        });
    }

    /**
     * 在页面关闭时移除监听
     */
    public void removeListener() {
        if (call != null) {
            call.cancel();
        }
        this.getContactListener = null;
        this.getUserRemindListListener = null;
        this.addRemindListener = null;
        this.updateRemindListener = null;
        this.delRemindListener = null;
        this.getAlbumListListener = null;
        this.delAlbumPictureListener = null;
    }

    public void setGetContactListener(MyInterface.GetContactListener getContactListener) {
        this.getContactListener = getContactListener;
    }

    public void setGetUserRemindListListener(MyInterface.GetUserRemindListListener getUserRemindListListener) {
        this.getUserRemindListListener = getUserRemindListListener;
    }

    public void setAddRemindListener(MyInterface.AddRemindListener addRemindListener) {
        this.addRemindListener = addRemindListener;
    }

    public void setUpdateRemindListener(MyInterface.UpdateRemindListener updateRemindListener) {
        this.updateRemindListener = updateRemindListener;
    }

    public void setDelRemindListener(MyInterface.DelRemindListener delRemindListener) {
        this.delRemindListener = delRemindListener;
    }

    public void setGetAlbumListListener(MyInterface.GetAlbumListListener getAlbumListListener) {
        this.getAlbumListListener = getAlbumListListener;
    }

    public void setDelAlbumPictureListener(MyInterface.DelAlbumPictureListener delAlbumPictureListener) {
        this.delAlbumPictureListener = delAlbumPictureListener;
    }

}
